package com.ossjk.oa.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ossjk.oa.system.vo.EmpDeptVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.system.entity.Bankcard;
import com.ossjk.oa.system.entity.Contacts;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.mapper.EmployeeMapper;
import com.ossjk.oa.system.service.IBankcardService;
import com.ossjk.oa.system.service.IContactsService;
import com.ossjk.oa.system.service.IEmployeeService;
import com.ossjk.oa.system.vo.EmpVo;
import com.ossjk.oa.system.vo.EmployeeVo;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author chair
 * @since 2019-01-14
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

	@Autowired
	private IContactsService iContactsService;
	@Autowired
	private IBankcardService iBankcardService;

	@Override
	public EmpVo selectOneEmpVoByID(@Param("id") String id      ) {
		return baseMapper.selectOneVoByID(id);
	}

	@Override
	public List<EmpVo> selectEmpVosByDid(@Param("did") String did    ){
		return baseMapper.selectEmpVosByDid(did);
	}

	@Override
	public List<EmpDeptVo> selectEmpDept(Wrapper wrapper) {
		return baseMapper.selectEmpDept(wrapper);
	}


	@Override
	public boolean updateDetail(Employee employee, String[] cname, String[] crelationship, String[] cphone,
								String[] bcard, String[] bbank, String[] bmain) {
		// 新增联系人
		if (!CommonUtil.isBlank(cname) && !CommonUtil.isBlank(crelationship) && !CommonUtil.isBlank(cphone)) {
			if (cname.length == crelationship.length && cname.length == cphone.length) {
				Wrapper wrapper = Condition.create().like("eid", employee.getId());
				iContactsService.delete(wrapper);
				List list = new ArrayList();
				for (int i = 0; i < cname.length; i++) {
					Contacts contacts = new Contacts();
					contacts.setName(cname[i]);
					contacts.setEid(employee.getId());
					contacts.setRelationship(new BigDecimal(crelationship[i]));
					contacts.setPhone(new BigDecimal(cphone[i]));
					list.add(contacts);
				}
				iContactsService.insertBatch(list);
			}
		}
		// 新增银行卡
		if (!CommonUtil.isBlank(bcard) && !CommonUtil.isBlank(bbank) && !CommonUtil.isBlank(bmain)) {
			if (bcard.length == bbank.length && bcard.length == bmain.length) {
				Wrapper wrapper = Condition.create().like("eid", employee.getId());
				iBankcardService.delete(wrapper);
				List list = new ArrayList();
				for (int i = 0; i < bcard.length; i++) {
					Bankcard bankcard = new Bankcard();
					bankcard.setCard(bcard[i]);
					bankcard.setEid(employee.getId());
					bankcard.setBank(bbank[i]);
					bankcard.setMain(new BigDecimal(bmain[i]));
					list.add(bankcard);
				}
				iBankcardService.insertBatch(list);
			}
		}
		// 更新基础信息
		return this.updateById(employee);
	}

	@Override
	public Page selectVoPage(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoPage(page, wrapper));
		return page;
	}

	@Override
	public List selectVoList(Wrapper wrapper) {
		return baseMapper.selectVoList(wrapper);
	}

	@Override
	public EmployeeVo selectVo(Wrapper wrapper) {
		// TODO Auto-generated method stub
		return baseMapper.selectVo(wrapper);
	}

}
